package br.edu.ifrn.sgp.resources;

import java.io.IOException;
import java.sql.SQLException;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import br.edu.ifrn.sgp.arq.util.EnvioArquivoUtils;

@Path("/arquivo")
public class ArquivoResource {
	
	@GET
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response baixarArquivo(@QueryParam("id") int id){
		
		if (id == 0)
			return Response.status(Response.Status.OK).entity(null).build();
		
		try {
			return EnvioArquivoUtils.getInstance().recuperaArquivoREST(id);
		} catch (SQLException e) {
			e.printStackTrace();
			return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
		} catch (IOException e) {
			e.printStackTrace();
			return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
		}
    }
}
